package com.example.android1.mytaxi.common.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
//import com.dalimao.mytaxi.common.databus.RxBus;
//import com.dalimao.mytaxi.common.http.biz.BaseBizResponse;
//import com.dalimao.mytaxi.common.lbs.LocationInfo;
//import com.dalimao.mytaxi.common.util.LogUtil;
//import com.dalimao.mytaxi.main.model.bean.Order;
//import com.dalimao.mytaxi.main.model.response.OrderStateOptResponse;
import com.example.android1.mytaxi.common.databus.RxBus;
import com.example.android1.mytaxi.common.http.biz.BaseBizResponse;
import com.example.android1.mytaxi.common.lbs.LocationInfo;
import com.example.android1.mytaxi.common.util.LogUtil;
import com.example.android1.mytaxi.main.model.bean.Order;
import com.example.android1.mytaxi.main.model.response.OrderStateOptResponse;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
//import cn.bmob.push.PushConstants;

/**
 * Created by liuguangli on 17/6/1.
 */
public class PushReceiver extends BroadcastReceiver {
    //位置发生变化
    private static final int MSG_TYPE_LOCATION = 1;
    // 订单变化
    private static final int MSG_TYPE_ORDER = 2;
    // PushConstants.ACTION_MESSAGE
    private String ACTION_MSG = "cn.bmob.push.PushConstants.ACTION_MESSAGE";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_MSG)) {
            String msg = intent.getStringExtra("msg");
            LogUtil.d("PushReceiver", "bmob 客户端收到推送内容：" + msg);
            //  通知业务或UI
            // {"data":
            //  {"key":"4913c896-2686-4230-86e5-5d9ae0f76c89",
            //  "latitude":23.135379999999998, "longitude":113.38665700000003, "rotation":-137.4028},
            // "type":1}
            try { //解析消息，业务处理
                JSONObject jsonObject = new JSONObject(msg);
                //type：1位置发生变化；2订单状态变化
                int type = jsonObject.optInt("type");
                if (type == MSG_TYPE_LOCATION) {
                    //1.司机位置变化，更新地图上司机UI
                    // 位置变化，解析json
                    LocationInfo locationInfo =
                            new Gson().fromJson(jsonObject.optString("data"), LocationInfo.class);
                    // RxBus发送数据，通知UI
                    RxBus.getInstance().send(locationInfo);

                } else if (type == MSG_TYPE_ORDER) {
    /* 2.订单状态变化
    {"type":2, 'data':{'cost': cost,'state': state, "ordarid":orderld, 'driverLatitude': lattude,
    'driverlongitude': longitude, 'driverRotation': rotation, 'key':uid,
    'drivorNamn':"abcd", "carNo":"湘B88888', "driverPhone":"15988888888"}}
    */
                    //  订单变化
                    // 解析数据.data对应订单信息，对应Order类。
                    Order order =
                            new Gson().fromJson(jsonObject.optString("data"), Order.class);
                    OrderStateOptResponse stateOptResponse = new OrderStateOptResponse();
                    stateOptResponse.setData(order);
                    stateOptResponse.setState(order.getState()); //包含ORDER_STATE_ACCEPT
                    stateOptResponse.setCode(BaseBizResponse.STATE_OK);
                    // 通知 UI，Presenter处理
                    RxBus.getInstance().send(stateOptResponse);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}


